{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 求任一实数平方根\n",
    "\n",
    "### 牛顿迭代法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.5\n",
      "1.4166666666666665\n",
      "1.4142156862745097\n",
      "1.4142135623746899\n"
     ]
    }
   ],
   "source": [
    "def sqrt_newton(a):\n",
    "    x = a/2\n",
    "    while abs(a-x*x) > 1e-6:\n",
    "        x1 = 0.5*(x + a/x)\n",
    "        print(x1)\n",
    "        x = x1\n",
    "    \n",
    "sqrt_newton(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.4142135623730951"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import math\n",
    "math.sqrt(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 二分法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1.0 2\n",
      "1.0 1.5 2\n",
      "1.0 1.25 1.5\n",
      "1.25 1.375 1.5\n",
      "1.375 1.4375 1.5\n",
      "1.375 1.40625 1.4375\n",
      "1.40625 1.421875 1.4375\n",
      "1.40625 1.4140625 1.421875\n",
      "1.4140625 1.41796875 1.421875\n",
      "1.4140625 1.416015625 1.41796875\n",
      "1.4140625 1.4150390625 1.416015625\n",
      "1.4140625 1.41455078125 1.4150390625\n",
      "1.4140625 1.414306640625 1.41455078125\n",
      "1.4140625 1.4141845703125 1.414306640625\n",
      "1.4141845703125 1.41424560546875 1.414306640625\n",
      "1.4141845703125 1.414215087890625 1.41424560546875\n",
      "1.4141845703125 1.4141998291015625 1.414215087890625\n",
      "1.4141998291015625 1.4142074584960938 1.414215087890625\n",
      "1.4142074584960938 1.4142112731933594 1.414215087890625\n",
      "1.4142112731933594 1.4142131805419922 1.414215087890625\n",
      "1.4142131805419922 1.4142141342163086 1.414215087890625\n"
     ]
    }
   ],
   "source": [
    "def sqrt_binary(a):\n",
    "    s = 0 \n",
    "    e = a\n",
    "    mid = (s+e) / 2\n",
    "    while abs(mid**2 - a) > 1e-6:\n",
    "        print(s, mid, e)\n",
    "        if mid**2 > a:\n",
    "            e = mid\n",
    "        else:\n",
    "            s = mid\n",
    "        mid = (s+e)/2\n",
    "        \n",
    "        \n",
    "sqrt_binary(2)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
